/*
 * Regole grammaticali:
 * <expr> → [PLUS | MINUS] <term> [(PLUS <term>) | (MINUS <term>)]*
 * 
 * Simboli terminali:
 * MINUS → "−"
 * PLUS → "+"
 */
package composite.number_expression;

import composite.Component;
import composite.NumberExpression;
import composite.number_expression.term.AddTerm;
import composite.number_expression.term.MinusTerm;
import java.util.LinkedList;
import java.util.List;
import visitor.Visitor;

/**
 *
 * @author Antonio
 */
public class Expr extends NumberExpression {

    private List<Component> term;

    public Expr(Component term) {

        if (term == null) {

            throw new NullPointerException();
        }

        this.term = new LinkedList();
        this.term.add(term);
    }

    @Override
    public void add(Component term) {

        if (term == null) {

            throw new NullPointerException();
        }

        this.term.add(term);
    }

    public List<Component> getStato() {

        return term;
    }// getStato

    @Override
    public void interpreta(String contesto) {

        for (Component te : term) {

            te.interpreta(contesto);
        }
    }

    @Override
    public void accept(Visitor v) {

        v.visit(this);
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();

        for (Component tr : term) {

            if (tr instanceof AddTerm) {

                sb.append(" + ");
            } else if (tr instanceof MinusTerm) {

                sb.append(" - ");
            }

            sb.append(tr);
        }
        return sb.toString();
    }
}
